package com.domain.controller;

import com.domain.common.Comments;
import com.domain.common.Const;
import com.domain.common.Result;
import com.domain.pojo.Users;
import com.domain.service.PublishService;
import com.domain.service.PublishTypeService;
import com.domain.utils.NotNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 发布问题 控制器
 */
@Controller
@RequestMapping("/publish")
public class PublishController {

    @Autowired
    private PublishTypeService publishTypeService;
    @Autowired
    private PublishService publishService;

    /**
     * 获取发布类型
     *
     * @param userToken
     * @return
     */
    @RequestMapping(value = "/getPublishType", method = RequestMethod.POST)
    @ResponseBody
    public Result getPublishType(String userToken, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return Result.success("操作成功", publishTypeService.getPublishType());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误！");
        }
    }

    /**
     * 发布帮忙
     *
     * @param userToken      用户token
     * @param userId         发布人id
     * @param typeId         发布类型id
     * @param publishTitle   发布标题
     * @param publishContent 发布内容
     * @param filterUserId   过滤好友id(多个逗号隔开)
     * @param longitude      发布位置经度
     * @param latitude       发布位置纬度
     * @param cityName       发布位置城市名称
     * @param itIsPaid       发布是否有偿
     * @param request
     * @param images         发布照片（多张照片逗号隔开）
     * @param money          帮转红包金额（没有传0.00）
     * @param
     * @return
     */
    @RequestMapping(value = "/savePublish", method = RequestMethod.POST)
    @ResponseBody
    public Result savePublish(String userToken,
                              String userId,
                              String typeId,
                              String publishTitle,
                              String publishContent,
                              String filterUserId,
                              String longitude,
                              String latitude,
                              String itIsPaid,
                              String cityName,
                              String images,
                              String money,
                              HttpServletRequest request) {

        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.savePublish(userId, typeId, publishTitle, publishContent, filterUserId, longitude, latitude, itIsPaid, cityName, images, money);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 完成帮忙判断
     *
     * @param userToken
     * @param userId
     * @param publishId
     * @param request
     * @return
     */
    @RequestMapping(value = "/judgeCompletePublish", method = RequestMethod.POST)
    @ResponseBody
    public Result judgeCompletePublish(String userToken, String userId, String publishId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.judgeCompletePublish(userId, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 完成帮忙
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param publishId 发布id
     * @param request
     * @return
     */
    @RequestMapping(value = "/completePublish", method = RequestMethod.POST)
    @ResponseBody
    public Result completePublish(String userToken, String userId, String publishId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.completePublish(userId, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统失败");
        }
    }

    /**
     * 帮转红包支付
     *
     * @param userToken    用户token
     * @param userId       用户id
     * @param body         对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。
     * @param subject      商品的标题/交易标题/订单标题/订单关键字等
     * @param total_amount 总金额
     * @param request
     * @return
     */
    @RequestMapping(value = "/helpRedPacakgePay", method = RequestMethod.POST)
    @ResponseBody
    public Result helpRedPacakgePay(String userToken, String userId, String body, String subject, String
            total_amount, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.helpRedPacakgePay(userId, body, subject, total_amount);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 发布补加红包支付
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param money     补加的钱数
     * @param publishId 发布id
     * @param body      对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。
     * @param subject   商品的标题/交易标题/订单标题/订单关键字等
     * @param request
     * @return
     */
    @RequestMapping(value = "/helpRedPacakgeSupplement", method = RequestMethod.POST)
    @ResponseBody
    public Result helpRedPacakgeSupplement(String userToken, String userId, String money, String publishId, String
            body, String subject, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.helpRedPacakgeSupplement(userId, money, publishId, body, subject);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

}
